package cn.net.shuxing.yifu.common.front.controller;

import cn.net.shuxing.yifu.common.bean.*;
import cn.net.shuxing.yifu.common.constant.UserType;
import cn.net.shuxing.yifu.common.dto.*;
import cn.net.shuxing.yifu.common.service.*;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/front/wallet")
public class MyWalletControlle extends BaseController {

    @Autowired
    private MemberWalletService memberWalletService;

    @Autowired
    private ShareholderWalletService shareholderWalletService;

    @Autowired
    private UserBankCardService userBankCardService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserCapitalService userCapitalService;

    @Autowired
    private UserDealHistoryService userDealHistoryService;

    @Autowired
    private FrontCommonService frontCommonService;

    @Autowired
    private DealParamService dealParamService;

    @Autowired
    private CashWalletService cashWalletService;

    @Autowired
    private JedisUtil jedisUtil;

    @Autowired
    private CoinTypeService coinTypeService;

    @Autowired
    private DealController dealController;

    @Autowired
    private RechargeHistoryService rechargeHistoryService;

    @Autowired
    private UserWithdrawalCashService userWithdrawalCashService;

    @Autowired
    private UserTransferService userTransferService;

    @Autowired
    private OpenPriceHistoryService openPriceHistoryService;

    @Autowired
    private ShareholderBuyGrantHistoryService shareholderBuyGrantHistoryService;

    @Autowired
    private ShortService shortService;

    /**
     * 钱包页面顶部蓝色部分
     *
     * @param userId
     * @return
     */
    @RequestMapping("/banner")
    public BaseDto getWalletBanner(Integer userId) {
        WalletBannerDto walletBannerDto = new WalletBannerDto();
        BaseDto baseDto = checkUserId(userId);
        if (baseDto != null && baseDto.isError())
            return baseDto;
        List<UserCapital> userCapitals = userCapitalService.getsByUserId(userId);
        if (userCapitals == null || userCapitals.isEmpty()) {
            return baseDto;
        }
        Integer type = userService.getByPrimaryKey(userId).getType();
        if (type == UserType.MEMBER || type == UserType.NONE) {
            walletBannerDto.setFrozenTotalCount(userCapitalService.getMemberFrozenTotalCount(userId));
            walletBannerDto.setUsableTotalCount(userCapitalService.getMemberUsAbleTotalCount(userId));
            walletBannerDto.setTotalCount(userCapitalService.getMemberTotalCapitalConvert(userId));
        } else if (type == UserType.SHAREHOLDER) {
            walletBannerDto.setFrozenTotalCount(userCapitalService.getShareholderFrozenTotalCount(userId));
            walletBannerDto.setTotalCount(userCapitalService.getShareholderTotalCapitalConvert(userId));
            walletBannerDto.setUsableTotalCount(userCapitalService.getShareholderUsAbleTotalCount(userId));
        }
        baseDto.setData(walletBannerDto);
        return baseDto;
    }

    /**
     * 钱包页面币种列表
     *
     * @param userId
     * @return
     */
    @RequestMapping("/coinList")
    public BaseDto getCoinList(Integer userId) {
        BaseDto baseDto = checkUserId(userId);
        if (baseDto != null && baseDto.isError())
            return baseDto;
        Integer type = userService.getByPrimaryKey(userId).getType();
        if (type == UserType.MEMBER || type == UserType.NONE) {
            List<WalletMemberCoinListDto> memberCoinListDto = coinTypeService.getMemberCoinListDto(userId);
            baseDto.setData(memberCoinListDto);
        } else if (type == UserType.SHAREHOLDER) {
            List<WalletShareholderCoinListDto> shareholderCoinListDto = coinTypeService.getShareholderCoinListDto(userId);
            baseDto.setData(shareholderCoinListDto);
        }

        return baseDto;
    }

    /**
     * 会员钱包币种对应交易记录
     *
     * @param userId
     * @param coinTypeId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/coinDealHistory")
    public BaseDto getCoinDealHistory(Integer userId,
                                      @RequestParam(value = "coinTypeId", defaultValue = "1") Integer coinTypeId,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        List<WalletCoinDealHistoryDto> walletCoinDealHistory = userDealHistoryService.getWalletCoinDealHistory(userId, coinTypeId, pageNum, pageSize);
        return BaseDto.ok(walletCoinDealHistory);
    }

    /**
     * 获取现金交易记录
     *
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/cashDealHistory")
    public BaseDto getCashDealHistory(Integer userId,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        boolean b = userService.checkUser(userId);
        if (!b) {
            return BaseDto.error("参数错误");
        } else {
            List<WalletCashDealHistoryDto> walletCashDealHistory = cashWalletService.getWalletCashDealHistory(userId, pageNum, pageSize);
            return BaseDto.ok(walletCashDealHistory);
        }
    }

    /**
     * 获取配发记录
     * @param userId
     * @param coinTypeId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/buyGrantHistory")
    public BaseDto getBuyGrantHistory(Integer userId,
                                      Integer coinTypeId,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return BaseDto.ok(shareholderBuyGrantHistoryService.getsWalletBuyGrantHistory(userId, coinTypeId, pageNum, pageSize));
    }
}